﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Shop : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                hdnShopID.Value = Session["SHOP_ID"].ToString();
                if (Session["USER_TYPE"].ToString() == "3")
                {
                    btnEdit.Visible = false;
                }
                BindDataShop();
                BindDataLocation();
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = "<br>" + ex.Message + "<br><br>";
        }
    }

    private void BindDataShop()
    {
        DataTable dt = new DataTable();
        DBConn oDBConn = new DBConn();
        oDBConn.OpenConnection();

        SqlCommand oCommand = new SqlCommand("sp_Deal_Shop_SELECTBYSHOPID", oDBConn.DBConnection);
        oCommand.CommandType = CommandType.StoredProcedure;
        oCommand.Parameters.Add("@Shop_ID", SqlDbType.BigInt).Value = hdnShopID.Value;

        SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
        oDataAdapter.Fill(dt);
        oDataAdapter = null;
        oDBConn.Disconnect();

        if (dt.Rows.Count > 0)
        {
            lblShopID.Text = dt.Rows[0]["Shop_ID"].ToString();
            lblShopName.Text = dt.Rows[0]["Shop_Name"].ToString();
            lblAddress.Text = dt.Rows[0]["Shop_Address"].ToString();
            lblLatitude.Text = dt.Rows[0]["Shop_Lat"].ToString();
            lblLongitude.Text = dt.Rows[0]["Shop_Long"].ToString();
            lblShopDetail.Text = dt.Rows[0]["Shop_Detail"].ToString();
            lblPackage.Text = dt.Rows[0]["Package_Name"].ToString();
            lblShopType.Text = dt.Rows[0]["ShopType_Name"].ToString();
            imgShopLogo.ImageUrl = dt.Rows[0]["Shop_Logo"].ToString();
            imgShop.ImageUrl = dt.Rows[0]["Shop_Image"].ToString();
        }
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        Response.Redirect("ShopDetail.aspx?ShopID=" + hdnShopID.Value + " ");
    }

    protected void btnAddLocation_Click(object sender, EventArgs e)
    {
        Response.Redirect("ShopLocation.aspx?ShopID=" + hdnShopID.Value + "&action=add");
    }

    private void BindDataLocation()
    {
        DataTable dt = new DataTable();
        DBConn oDBConn = new DBConn();
        oDBConn.OpenConnection();

        SqlCommand oCommand = new SqlCommand("sp_Deal_Location_SELECTBYSHOPID", oDBConn.DBConnection);
        oCommand.CommandType = CommandType.StoredProcedure;
        oCommand.Parameters.Add("@Shop_ID", SqlDbType.BigInt).Value = hdnShopID.Value;

        SqlDataAdapter oDataAdapter = new SqlDataAdapter(oCommand);
        oDataAdapter.Fill(dt);
        oDataAdapter = null;
        oDBConn.Disconnect();

        if (dt.Rows.Count > 0)
        {
            grdLocation.AllowPaging = true;
            grdLocation.DataSource = dt;
            grdLocation.DataBind();

            grdLocation.Visible = true;
            btnAddOneLocation.Visible = false;
        }
        else
        {
            grdLocation.Visible = false;
            btnAddOneLocation.Visible = true;
            lblLocation.Visible = true;
            lblLocation.Text = "กรุณากรอกข้อมูลสาขาหรือข้อมูลร้าน";
        }
    }

    private int idxRow;
    protected void grdLocation_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            //Binding.GridViewRowDataBound()
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblNo = (Label)e.Row.FindControl("lblNo");
                lblNo.Text = idxRow.ToString();

                Label lblID = (Label)e.Row.FindControl("lblID");

                ImageButton btnLocationView = (ImageButton)e.Row.FindControl("btnLocationView");
                btnLocationView.PostBackUrl = "ShopLocation.aspx?ShopID=" + hdnShopID.Value + "&action=view&LocationID=" + lblID.Text;

                ImageButton btnLocationEdit = (ImageButton)e.Row.FindControl("btnLocationEdit");
                btnLocationEdit.PostBackUrl = "ShopLocation.aspx?ShopID=" + hdnShopID.Value + "&action=edit&LocationID=" + lblID.Text;

                ImageButton btnLocationDelete = (ImageButton)e.Row.FindControl("btnLocationDelete");
                btnLocationDelete.Attributes.Add("onclick", "return confirm('ยืนยันการลบข้อมูลสาขาและข้อมูลโปรโมชั่นของสาขา?');");
            }
            idxRow++;
        }
        catch (Exception ex)
        {
            lblLocation.Text = ex.Message;
        }
    }

    protected void grdLocation_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Del")
            {
                string strKey = e.CommandArgument.ToString();
                DBConn oDBConn = new DBConn();
                oDBConn.OpenConnection();

                SqlCommand oCommand = new SqlCommand("sp_Deal_Location_DELETE", oDBConn.DBConnection);
                oCommand.CommandType = CommandType.StoredProcedure;
                oCommand.Parameters.Add("@Location_ID", SqlDbType.BigInt).Value = strKey;
                Int32 intLocation = oCommand.ExecuteNonQuery();

                oDBConn.Disconnect();

                lblLocation.Text = "<br>ลบข้อมูลเรียบร้อยแล้ว<br>";
                BindDataLocation();
            }
        }
        catch (Exception ex)
        {
            lblLocation.Text = ex.Message;
        }
    }
    protected void btnAddOneLocation_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = new DataTable();
            DBConn oDBConn = new DBConn();
            oDBConn.OpenConnection();

            SqlCommand oCommand = new SqlCommand("sp_Deal_Location_INSERT", oDBConn.DBConnection);
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.Parameters.Add("@Location_Name", SqlDbType.VarChar).Value = lblAddress.Text.Trim();
            oCommand.Parameters.Add("@Location_Detail", SqlDbType.VarChar).Value = lblShopDetail.Text.Trim();
            oCommand.Parameters.Add("@Location_Lat", SqlDbType.Float).Value = lblLatitude.Text.Trim();
            oCommand.Parameters.Add("@Location_Long", SqlDbType.Float).Value = lblLongitude.Text.Trim();
            oCommand.Parameters.Add("@Location_Shop_ID", SqlDbType.BigInt).Value = hdnShopID.Value;

            oCommand.ExecuteNonQuery();
            oDBConn.Disconnect();

            Response.Redirect("Shop.aspx");
        }
        catch (Exception ex)
        {
            lblLocation.Text = ex.Message;
        }
    }
}
